package com.learn.koom;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.kwai.koom.base.MonitorConfig;
import com.kwai.koom.base.MonitorManager;
import com.kwai.koom.javaoom.monitor.OOMHprofUploader;
import com.kwai.koom.javaoom.monitor.OOMMonitor;
import com.kwai.koom.javaoom.monitor.OOMMonitorConfig;
import com.learn.koom.test.LeakMaker;

import org.jetbrains.annotations.NotNull;

import java.io.File;

import androidx.appcompat.app.AppCompatActivity;

public class JavaLeakActivity extends AppCompatActivity {

    private Button btn_java;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_java_leak);
        btn_java = (Button) findViewById(R.id.btn_java);
        btn_java.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MonitorConfig config = new OOMMonitorConfig.Builder()
                        .setThreadThreshold(50) //50 only for test! Please use default value!
                        .setFdThreshold(300) // 300 only for test! Please use default value!
                        .setHeapThreshold(0.9f) // 0.9f for test! Please use default value!
                        .setVssSizeThreshold(1_000_000) // 1_000_000 for test! Please use default value!
                        .setMaxOverThresholdCount(1) // 1 for test! Please use default value!
                        .setAnalysisMaxTimesPerVersion(3) // Consider use default value！
                        .setAnalysisPeriodPerVersion(15 * 24 * 60 * 60 * 1000) // Consider use default value！
                        .setLoopInterval(5_000) // 5_000 for test! Please use default value!
                        .setEnableHprofDumpAnalysis(true)
                        .setHprofUploader(new OOMHprofUploader() {
                            @Override
                            public void upload(@NotNull File file, @NotNull HprofType hprofType) {
                                Log.e( "upload: ", file.getAbsolutePath());
                            }
                        })
                        .build();

                MonitorManager.addMonitorConfig(config);
                OOMMonitor.INSTANCE.startLoop(true, false, 5_000L);
                LeakMaker.makeLeak(JavaLeakActivity.this);
            }
        });

    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, JavaLeakActivity.class));
    }
}